package fr.miage.shopping.presentation.panel;

import fr.miage.shopping.dao.traitements.CategorieDao;
import fr.miage.shopping.metier.entite.Categorie;
import fr.miage.shopping.metier.impl.CategorieBusinessServiceImpl;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import java.util.Set;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.tree.TreeSelectionModel;
/**
 *
 * @author vincent
 */
public class Menu2 extends javax.swing.JPanel implements TreeSelectionListener {

    private ListProduit lp;

    /** Creates new form Menu */
    public Menu2() {

        initComponents();

    }

    private void initComponents() {

    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Nos Produits");

    DefaultMutableTreeNode child;
    DefaultMutableTreeNode grandChild;
    DefaultMutableTreeNode grandgrandChild;

    CategorieBusinessServiceImpl cb = new CategorieBusinessServiceImpl();

    List<Categorie> list = cb.getAllCategorie();
      List<Categorie> listPere = CategorieDao.selectAllCategoriePere();
    for (Categorie cat: list){
         for(int i=0;i<listPere.size();i++){
             if(cat.getIdCate()==listPere.get(i).getIdCate()){
          
            child = new DefaultMutableTreeNode(cat.getNomCate());
            root.add(child);
            Set childlist = cat.getCategories();
            List<Categorie> listchild = new ArrayList<Categorie>(childlist);

            for(Categorie cat2: listchild) {
                grandChild = new DefaultMutableTreeNode(cat2.getNomCate());
                child.add(grandChild);
                 if (!cat2.getCategories().isEmpty()){
                     Set grandchildlist = cat2.getCategories();
                     List<Categorie> grandlistchild = new ArrayList<Categorie>(grandchildlist);
                     for(Categorie cat3: grandlistchild) {
                        grandgrandChild = new DefaultMutableTreeNode(cat3.getNomCate());
                        grandChild.add(grandgrandChild);
                     }
                 }
           }
        }
         }
    }

        jScrollPane1 = new javax.swing.JScrollPane();
        jTree1 = new JTree(root);
        jTree1.addTreeSelectionListener(this);

        jTree1.getSelectionModel().setSelectionMode
            (TreeSelectionModel.SINGLE_TREE_SELECTION);




        jScrollPane1.setViewportView(jTree1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 500, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(14, Short.MAX_VALUE))
        );
    }

    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTree jTree1;

    public JScrollPane getJScrollPane1() {
        return jScrollPane1;
    }

    public void valueChanged(TreeSelectionEvent e) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode)
                           jTree1.getLastSelectedPathComponent();

        if (node == null) return;

        /* retrieve the node that was selected */

        Object nodeInfo = (Object) node.getUserObject();
        
        //dernier element
        if (node.isLeaf()) {
               CategorieBusinessServiceImpl cb = new CategorieBusinessServiceImpl();
                List<Categorie> list = cb.getAllCategorie();
                Categorie cate = null;
                for (int i = 0;i<list.size();i++){
                    if(list.get(i).getNomCate().equalsIgnoreCase(nodeInfo.toString())){
                        cate = list.get(i);
                    }
                }
            lp.chargement(cate);
        } 
    }

    public final void setListeProduit(final ListProduit valeur){
        this.lp = valeur;
    }
}
